var dataGrid;
var ctx;
var dictionarytypeTree;
var myFilePath;
var myId;
var materialType;
var loginName;
	$(function() {
		dictionarytypeTree = $('#dictionarytypeTree').tree({
			url : ctx + '/ebook/tree',
			parentField : 'pid',
			lines : true,
			onClick : function(node) {
				filePath = node.attributes.split('&')[0];
				materialType = node.attributes.split('&')[1];
				myId = node.id;
				myFilePath = node.attributes.split('&')[0];
				parent.filePath = node.attributes.split('&')[0];
				parent.id = node.id;
				parent.materialType =  node.attributes.split('&')[1];
 				dataGrid.datagrid('load', {
				    id: node.id,
				});
 			}
		});
		dataGrid = $('#dataGrid').datagrid({
			url : ctx + '/ebook/dataGridForMaterial',
			striped : true,
			rownumbers : true,
			pagination : true,
			singleSelect : true,
			idField : 'id',
			sortName : 'id',
			sortOrder : 'asc',
			pageSize : 50,
			pageList : [ 10, 20, 30, 40, 50, 100, 200, 300, 400, 500 ],
			frozenColumns : [ [{
				width : '150',
				title : '文档名',
				field : 'name',
				align : 'center',
			}, {
				width : '150',
				title : '大小（KB）',
				field : 'size',
				align : 'center',
			} , {
				width : '150',
				title : '上传者',
				field : 'builderName',
				align : 'center',
			},{
				width : '80',
				title : '档案类型',
				align : 'center',
				field : 'materialType',
				formatter : function(value, row, index) {
					if (value) {
						return "文件";
					}else{
						return "文件夹";
					}
				}
			}, {
				width : '150',
				title : '描述',
				align : 'center',
				field : 'description',
			},{
				field : 'action',
				title : '操作',
				width : 150,
				align : 'center',
				formatter : function(value, row, index) {
					var str = '&nbsp;';
					fileName = encodeURI(row.name); // 进行URL编码
					fileName = encodeURI(fileName); 
						if (row.materialType == 1) {
							str = "<a href='" + ctx + "/ebook/download?path="+ row.url +"&fileName=" + row.name + "'>下载</a>";
						}
						if (loginName == 'admin'){
							str += $.formatString('&nbsp;&nbsp;<a href="javascript:void(0)" onclick="deleteFun(\'{0}\');" >删除</a>', row.id);
//							str += "&nbsp;&nbsp;<a href='" + ctx + "/ebook/delete?id="+ row.id + "'>删除</a>";
						}
					return str;
				}
			} ] ],
			toolbar : '#toolbar',
		});
	});
	
	function addFun() {
		if(filePath == "" || filePath == null){
			$.messager.alert('警告','请选择文件夹'); 
			return false;
		}else if(materialType != 0){
			$.messager.alert('警告','请选择文件夹'); 
			return false;
		}
		var href = ctx + '/ebook/uploadVideo?myFilePath=' + myFilePath + '&myId=' + myId;
		href = encodeURI(href); // 进行URL编码
		href = encodeURI(href); 
		parent.$.modalDialog({
			title : '文件上传',
			width : 500,
			height : 250,
			href : href,
			buttons : [ {
						text : '上传',
						handler : function() {
							parent.$.modalDialog.openner_tree = dictionarytypeTree;
							var f = parent.$.modalDialog.handler.find('#roleAddForm');
							f.submit();
						}
					} ]
		});
	}
	function newFun() {
		if(filePath == "" || filePath == null){
			$.messager.alert('警告','请选择文件夹');
			return false;
		}else if(materialType != 0){
			$.messager.alert('警告','请选择文件夹');
			return false;
		}
		var href = ctx + '/ebook/newFolderUI?myFilePath=' + myFilePath + '&myId=' + myId;
		href = encodeURI(href); // 进行URL编码
		href = encodeURI(href); 
		parent.$.modalDialog({
			title : '新建文件夹',
			width : 500,
			height : 180,
			href : href,
			buttons : [ {
				text : '确定',
				handler : function() {
					parent.$.modalDialog.openner_tree_2 = dictionarytypeTree;
					var f = parent.$.modalDialog.handler.find('#newFolderForm');
					f.submit();
				}
			} ]
		});
	}
	
	function deleteFun(id) {
		parent.$.messager.confirm('询问', '您是否要删除当前档案？', function(b) {
			if (b) {
				//progressLoad();
				$.post(ctx + '/ebook/delete', {
					id : id
				}, function(result) {
					if (result.success) {
						parent.$.messager.alert('提示', result.msg, 'info');
						var tab = parent.$('#index_tabs').tabs('getSelected');  // get selected panel
						tab.panel('refresh');
					}else{
						parent.$.messager.alert('提示', result.msg, 'info');
					}
					//progressClose();
				}, 'JSON');
			}
		});
	}
	
function editFun(id) {
	parent.$.modalDialog({
		title : '文件上传',
		width : 500,
		height : 350,
		href : ctx + '/ebook/uploadVideo?',
		buttons : [ {
					text : '确定',
					handler : function() {
//						parent.$.modalDialog.openner_dataGrid = dataGrid;// 因为添加成功之后，需要刷新这个dataGrid，所以先预定义好
						var f = parent.$.modalDialog.handler.find('#roleAddForm');
						f.submit();
					}
				} ]
	});
}
	
